package com.zxl.always.on.display.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Created by makai on 15-7-1.
 */
public abstract class HomeWatcherReceiver extends BroadcastReceiver {

	final static String SYSTEM_DIALOG_REASON_KEY = "reason";
	final static String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
	final static String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
	final static String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

	private boolean mRegist = false;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
			String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
			if (reason != null) {
				if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
					onHomeClick();
				}
			}
		}
	}

	public void regist(Context context) {
		if (mRegist) {
			return;
		}
		try {
			context.registerReceiver(this, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
			mRegist = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void unregist(Context context) {
		if (!mRegist) {
			return;
		}
		try {
			context.unregisterReceiver(this);
			mRegist = false;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 按home键
	 */
	public abstract void onHomeClick();
}
